<template>
	<view>
		<view class="top-back">
			<view class="top-name">服务权益（{{count}}）</view>
			<view class="top-right  top-right-left" @click="importAction">
				<view class="top-exit">导入</view>
			</view>
			<view class="top-right">
				<view class="top-exit" @click="exportAction">导出</view>
			</view>
			<view class="top-right" :class="screenSelect?'top-right-select':''" @click="selectAction">
				<view :class="screenSelect?'top-contnet top-content-select': 'top-contnet'">筛选</view>
				<image v-if="!screenSelect" class="top-icon" src="../../../static/select.png"></image>
				<image v-if="screenSelect" class="top-icon" src="../../../static/select_select.png"></image>

			</view>
		</view>
		<view class="uni-container">
			<uni-table ref="table" :loading="loading" border stripe emptyText=" ">
				<uni-tr>
					<uni-th align="center" width="60px">排序</uni-th>
					<uni-th align="center" width="80px">服务权益id</uni-th>
					<uni-th align="center">关联订单</uni-th>
					<uni-th align="center" width="120px">关联客户</uni-th>
					<uni-th align="center">有效期</uni-th>
					<uni-th align="center">可咨询数</uni-th>
					<uni-th align="center">可咨询领域</uni-th>
					<uni-th align="center">已咨询次数</uni-th>
					<uni-th align="center">到期时间</uni-th>
					<uni-th align="center">服务状态</uni-th>
					<uni-th align="center">操作</uni-th>

				</uni-tr>
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td align="center">{{index + 1}}</uni-td>
					<uni-td align="left">
						<view @click="detailAction(item)"><text selectable="true">{{item.no}}</text></view>
					</uni-td>
					<uni-td align="left"><text selectable="true">{{ item.orderNo}}</text></uni-td>
					<uni-td align="left"><text selectable="true">{{ item.custName }}</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.totalDay }}天</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.totalCount}}</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.domainDict}}</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.usedCount}}</text></uni-td>
					<uni-td align="left"><text selectable="true">{{ item.endTime}}</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.status.desc}}</text></uni-td>
					<uni-td align="center">
						<view style="display: flex;justify-content: center;">
							<view class="item-blue-text" @click="detailAction(item)">详情</view>
							<view style="margin-left: 12px;" v-if="item.status.code == 0 || item.status.code == 1 "
								@click="cancelAction(item)" class="item-blue-text">修改</view>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>

			<view class="search-back" v-if="tableData.length == 0">
				<image class="search-image" :src="pc_search_nodata"></image>
				<view class="search-noresult">没有查询到结果</view>
			</view>
			<view style="margin-top: 10px;margin-bottom: 10px;" v-if="tableData.length > 0">
				<uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="count"
					@change="pageChange" />
			</view>
		</view>
		<serveImport v-on:successBack='updataRequest()' v-if="showImport" @importServeClose='clickImportServeClose'>
		</serveImport>
		<serveEdit v-if="showChange" :itemID="selectItem.id" :rangeList='rangeList' v-on:successBack='updataRequest()'
			@serveEditClose='clickServeEditClose'></serveEdit>
		<serveDetail v-if="showDetail" :item='selectItem' @serveDetailClose='clickServeDetailClose'></serveDetail>
		<popSelect v-model="showScreen" :x="screenx" :y="screeny" placement="top-end" dynamic>
			<view v-if="showScreen" class="screen-back">
				<uni-forms ref="baseForm" :modelValue="popData" style="width:200px;">
					<uni-forms-item>
						<uni-easyinput v-model="popData.id" placeholder="服务权益id" />
					</uni-forms-item>
					<uni-forms-item>
						<uni-easyinput v-model="popData.orderId" placeholder="关联订单" />
					</uni-forms-item>
					<uni-forms-item>
						<uni-easyinput v-model="popData.custName" placeholder="关联客户" />
					</uni-forms-item>
					<uni-forms-item>
						<selectxy :zindex=399 ref="selectxy" :value="multipleArr" placeholder="服务状态"
							:options="serveList" @change="multipleChange">
						</selectxy>
					</uni-forms-item>
					<uni-forms-item>
					</uni-forms-item>
					<view class="select-bottom">
						<view class="select-bottom-left" @click="selectReset">重置</view>
						<view class="select-bottom-right" @click="selectSureAction">筛选</view>
					</view>
				</uni-forms>
			</view>
		</popSelect>

	</view>
</template>

<script>
	import popSelect from '@/components/popSelect/popSelect.vue'
	import uniselect from "@/packageB/PC/components/uni-data-select/uni-data-select.vue"
	import selectxy from "@/components/select-cy/select-cy.vue"
	import serveDetail from "@/packageB/PC/orderServe/serveDetail.vue"
	import serveEdit from "@/packageB/PC/orderServe/serveEdit.vue"
	import serveImport from "@/packageB/PC/orderServe/serveImport.vue"
	export default {
		components: {
			popSelect,
			uniselect,
			serveDetail,
			selectxy,
			serveEdit,
			serveImport
		},
		data() {
			return {
				showDetail: false,
				tableData: [],
				screenx: 0,
				screeny: 0,
				showScreen: false,
				popData: {
					'no': '',
					'orderId': '',
					'custName': "",
					'status': ''
				},
				screenSelect: false,
				count: 0, // 数据总量
				pageSize: 10, // 每页数据量
				pageCurrent: 1, // 当前页
				selectItem: {},
				rangeList: [],
				serveList: [],
				multipleArr: [],
				selectArr: [],
				showChange: false,
				showImport: false,
				pc_search_nodata: getApp().globalData.baseImageUrl + '/pc_search_nodata.png'
			}
		},

		mounted() {
			this.requestData();
			this.requestDicData();
			this.requestDic1Data();
		},
		methods: {
			// 分页触发
			pageChange(e) {
				this.pageCurrent = e.current
				this.requestData()
			},
			//网络请求示例
			async requestData() {
				let _this = this
				let parmas = {
					size: _this.pageSize,
					page: _this.pageCurrent,
				};
				var url = "/admin/customerRights/list";
				if (this.screenSelect) {
					if (_this.popData.no) {
						this.$set(parmas, 'no', _this.popData.no)
					}
					if (_this.popData.orderId) {
						this.$set(parmas, 'orderId', _this.popData.orderId)
					}
					if (_this.popData.custName) {
						this.$set(parmas, 'custName', 'lk:' + _this.popData.custName)
					}
					if (_this.selectArr.length) {
						var dicStr = '';
						for (var i = 0; i < _this.selectArr.length; i++) {
							if (i == 0) {
								dicStr = _this.selectArr[i].value
							} else {
								dicStr += "," + _this.selectArr[i].value
							}
						}
						this.$set(parmas, 'status', 'in:' + dicStr)
					}
				}

				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.count = res.data.data.count;

					var arr = [];
					for (var i = 0; i < res.data.data.list.length; i++) {
						var item = res.data.data.list[i];
						//权益总数量
						item.totalCount = item.totalCount == -1 ? '不限' : item.totalCount
						//领域
						var str = item.domainDict
						if (str.includes('-1')) {
							var str1 = str.replace(/-1/g, '不限')
							item.domainDict = str1
						}
						arr.push(item)
					}
					_this.tableData = arr;
				}
			},
			//导出
			exportAction() {
				uni.downloadFile({
					url: getApp().globalData.baseUrl + "/admin/customerRights/export",
					header: {
						"Content-Type": "application/x-xls",
						"Authorization": "Basic ZG90Z286ZG90Z28=",
						"token": uni.getStorageSync("accessToken") ? uni.getStorageSync("accessToken") : ''
					},
					success: function(res) {
						wx.hideLoading();
						uni.saveFile({
							tempFilePath: res.tempFilePath, //临时路径
							success: (resp) => {
								uni.openDocument({
									filePath: resp.savedFilePath,
									fileType: 'xls',
									showMenu: true,
									success: function(res) {
										console.log('打开文档成功')
									},
								});
							}
						})
					},
					fail: function(err) {
						wx.hideLoading();
						console.log('下载失败：', err);
					},
				});
			},
			//咨询领域
			async requestDic1Data() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/param_consult_domain",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.rangeList = arr;
					console.log(_this.rangeList)
				}
			},
			//状态
			async requestDicData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/$Status$customerRights",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.serveList = arr;
					console.log(_this.serveList)
				}
			},

			async updataRequest() {
				this.requestData()
			},
			//点击筛选
			selectAction(e) {
				this.screenx = e.touches[0].clientX
				uni.getSystemInfo({
					success: res => {
						this.screenx = res.windowWidth - 30
					}
				})
				this.screeny = e.touches[0].clientY + 4
				this.showScreen = !this.showScreen
			},

			//筛选重置
			selectReset() {
				this.popData = {
					'no': '',
					'orderId': '',
					'custName': "",
					'status': ''
				};
				this.$refs.selectxy.handleAllRemove();
				this.screenSelect = false;
				this.requestData();

			},

			//咨询领域
			multipleChange(item, value) {
				console.log('咨询领域' + item, value);
				this.selectArr = item;
				this.multipleArr = value;
			},

			//筛选确认	
			selectSureAction() {

				if (this.popData.no == '' && this.popData.goodsName == '' && this.popData.custName == '' && this.popData
					.buyerName == '') {
					uni.showToast({
						title: '请设置筛选条件',
						icon: "none"
					})
				} else {
					this.screenSelect = true;
					this.showScreen = !this.showScreen
					this.requestData()
				}
			},
			//详情
			detailAction(item) {
				console.log(item.id);
				this.selectItem = item;
				this.showDetail = true;
			},
			//取消
			cancelAction(item) {
				this.selectItem = item;
				this.showChange = true;
			},

			clickServeEditClose() {
				this.showChange = false;
			},

			//导入
			importAction() {
				this.showImport = true;
			},
			clickServeDetailClose() {
				this.showDetail = false;
			},
			clickImportServeClose() {
				this.showImport = false;
			},
		}
	}
</script>

<style scoped>
	.top-back {
		display: flex;
		align-items: center;
		padding: 10px;
	}

	.top-name {
		color: #333;
		font-size: 18px;
		font-weight: bold;
		margin-left: 30px;
	}

	.top-num {
		color: #999;
		font-size: 18px;
		margin-left: 4px;
	}

	.top-contnet {
		font-size: 14px;
		color: #999;
	}

	.top-content-select {
		color: #2DA9C5;
	}

	.top-icon {
		width: 16px;
		height: 16px;
	}

	.top-right {
		cursor: pointer;
		display: flex;
		align-items: center;
		margin-right: 16px;
		padding: 5px 10px;
		border-radius: 5px;
	}

	.top-right-select {
		background-color: #E8F7FB;
	}

	.top-right-left {
		margin-left: auto;
	}

	.uni-container {
		margin-left: 40px;
		margin-right: 16px;
		width: auto;
	}

	.search-back {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.search-noresult {
		font-size: 14px;
		color: #333;
		font-weight: 400;
	}

	.search-image {
		width: 150px;
		height: 150px;
	}

	.screen-back {
		padding: 10px;
		width: 300px;
	}

	.header-img {
		width: 35px;
		height: 35px;
		border-radius: 50%;
		vertical-align: middle;
	}

	.item-blue-text {
		cursor: pointer;
		font-size: 14px;
		font-weight: 400;
		color: #2DA9C5;
	}

	.top-exit {
		width: 56px;
		height: 30px;
		background: #E8F7FB;
		border-radius: 2px;
		font-size: 14px;
		font-weight: normal;
		color: #2DA9C5;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.select-bottom {
		display: flex;
	}

	.select-bottom-left {
		cursor: pointer;
		height: 32px;
		width: 120px;
		color: #333;
		background: #FFFFFF;
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 14px;
	}

	.select-bottom-right {
		cursor: pointer;
		height: 32px;
		width: 120px;
		color: #fff;
		background: #8ED8E9;
		border-radius: 2px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 14px;
		margin-right: 0px;
		margin-left: auto;
	}
</style>